//
//  DatePicker.m
//  nova
//
//  Created by user on 11/06/21.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "DatePicker.h"


@implementation DatePicker

@synthesize targetTimeSelector;
@synthesize targetObject;
@synthesize targetButton;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	[self.view setBackgroundColor: [UIColor clearColor]];
    [super viewDidLoad];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (IBAction) closeView{
	[self.view removeFromSuperview];
}



-(IBAction)done:(id)sender{
	//Use NSDateFormatter to write out the date in a friendly format
	//[df setDateStyle:NSDateFormatterMediumStyle];	
	//[df setTimeStyle:NSDateFormatterNoStyle];DateFormat
	//NSLocale *currentUsersLocale = [NSLocale currentLocale];
	//NSDateFormatter *df = [[NSDateFormatter alloc] init];
	//*self.targetDate = [[datePicker date] timeIntervalSince1970];
	//NSDate data  = [NSDate dateWithTimeIntervalSince1970:targetDate];
	//[targetAction targetTimeSelector : [[datePicker date] timeIntervalSince1970]];
	//[targetAction respondsToSelector:targetTimeSelector:[[datePicker date] timeIntervalSince1970]];
	
	
	//Ver http://theocacao.com/document.page/264
	//Metodo genérico para para poder alterar qualquer data no objecto Action.
	
	double timeInterval =  [[datePicker date] timeIntervalSince1970];	
	NSMethodSignature * sig = nil;
	NSInvocation * myInvocation = nil;
	sig = [[targetObject class] instanceMethodSignatureForSelector:targetTimeSelector];	
	myInvocation = [NSInvocation invocationWithMethodSignature:sig];
	[myInvocation setArgument:&timeInterval atIndex:2];
	[myInvocation setTarget:targetObject];
	[myInvocation setSelector:targetTimeSelector];
	[myInvocation retainArguments];	
	[myInvocation invoke];
	[targetButton setTitle:[Library getLocalizedDateString : timeInterval] forState: UIControlStateNormal ];
	//NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:@"EdMMMYYYY" options:0 locale:currentUsersLocale];
	//[df setDateFormat:dateFormat];
	//targetTextField.text = [NSString stringWithFormat:@"%@",  [df stringFromDate:[datePicker date]]]; 
	
	//[targetTextField resignFirstResponder];
	
	//targetTextField = nil;
	//[targetTextField release];
	//[self.view removeFromSuperview];
	//targetDate = nil;
	//[targetDate release];
}

- (void)dealloc {
    [super dealloc];
}


@end
